import asyncio
from typing import Optional
from contextlib import AsyncExitStack
from unittest import result

from fastmcp.client import Client
from mcp import ClientSession

from dotenv import load_dotenv
from pydantic.deprecated import tools

load_dotenv()  # 从 .env 加载环境变量

class MCPClient:
    def __init__(self):
        # 初始化会话和客户端对象
        self.session: Optional[ClientSession] = None
        self.exit_stack = AsyncExitStack()
    # 方法将在这里添加
    
MCP_BASE_URL = "http://localhost:8000/mcp"  # 根据实际服务地址调整

async def connect_to_server(server_url: str):
    """连接到 MCP 服务器

    Args:
        server_url: MCP 服务器的 URL
    """

    async with Client(server_url) as client:
        tools = await client.list_tools()
        print(f"可用工具：{tools}")
        
        result = await client.call_tool("get_weather_info",{"city":"成都"})
        print(result)

async def get_tools(server_url: str):
    client = Client(server_url)
    await client.__aenter__()
    tools = await client.list_tools()
    print(f"可用工具：{tools}")
    return client, tools

if __name__ == "__main__":
    asyncio.run(get_tools(MCP_BASE_URL))
    
    